package pers.cmc.mysql.core;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 反射
 * @author EDY
 */
public class Reflex {

    /** 获取一个类的所有成员变量 */
    public static Map<String,Object> getMembers(Object obj) throws IllegalAccessException {
        Field[] fields = obj.getClass().getDeclaredFields();
        Map<String,Object> map = new HashMap<>();
        for(Field field : fields){
            System.out.println(field.getName());
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            map.put(field.getName(),field.get(obj));
        }
        return map;
    }

    public static void setMember(Object tar,String name,Object obj) throws IllegalAccessException {
        Field[] fields = tar.getClass().getDeclaredFields();
        for(Field field : fields){
            if (!field.isAccessible()) {
                field.setAccessible(true);
            }
            if(field.getName().equals(name)){
                field.set(tar, obj);
                return;
            }
        }
    }

}
